# coding:utf-8
# @Time    : 2021/12/30 15:40
# @Author  : Martin
from page_object.base import BasePage


class DetailPage(BasePage):

    def click_res_detail(self):
        self.scroll_center()
        self.click('id', 'resourceDetail')

    def get_resource_amount(self, company, bu=None, brand=None, store=None, **kwargs):

        table = self.locate('css selector', '#detailedtableMultiProperProvider')
        rows = table.find_element('tag name', 'tbody').find_elements('tag name', 'tr')

        for row in rows:
            # 获取当前资源行所有信息
            cur_params = {}
            # 入参资源行信息
            params = {}
            tds = row.find_elements('tag name', 'td')

            if company:
                cur_company = tds[1].find_element('tag name', 'input').get_attribute('value')
                cur_params['company'] = cur_company
                params['company'] = company
            if bu:
                cur_bu = tds[2].find_elements('tag name', 'input')[1].get_attribute('value')
                cur_params['bu'] = cur_bu
            if brand:
                # 获取事业部
                cur_brand = tds[4].find_element('tag name', 'input').get_attribute('value')
                cur_params['cur_brand'] = cur_brand
                params['cur_brand'] = brand
            if store:
                # 获取门店值
                cur_store = tds[2].find_elements('tag name', 'input')[1].get_attribute('value')
                cur_params['store'] = cur_store
                params['store'] = store
            # 行信息匹配，则获取额度信息
            if cur_params == params:
                # 获取工厂资源
                # fac_amount = row.find_elements('xpath', '//td/input[@name="multiProperNoShopProvider'
                #                                         '.factoryResourceAmount"]')[rows.index(row)].get_attribute('value')
                # # 获取我司资源
                # com_amount = row.find_elements('xpath', '//td/input[@name="multiProperNoShopProvider'
                #                                         '.selfResourceAmount"]')[rows.index(row)].get_attribute('value')
                # 获取工厂资源
                fac_amount = tds[-7].find_element('tag name', 'input').get_attribute('value')
                # 获取我司资源
                com_amount = tds[-3].find_element('tag name', 'input').get_attribute('value')
                self.logger.info(f'获取实际资源额度，行信息为{cur_params}，工厂剩余额度{fac_amount}，我司剩余额度{com_amount}')
                return fac_amount, com_amount

        return '不存在预期的资源维度行信息'